/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bm.actions;

import bm.data.Coche;
import bm.forms.CocheForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author Anita
 */

public class AddCoche extends Action{


    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        System.out.println("addcoche action file");
        CocheForm cocheForm = (CocheForm) form;
        Coche coche = cocheForm.getCoche();

        this.almacenarCoche(coche);

        request.getSession().setAttribute("marca", coche.getMarca());
        request.getSession().setAttribute("modelo", coche.getModelo());
        request.getSession().setAttribute("caracteristicas", coche.getCaracteristicas());
        request.getSession().setAttribute("precio", coche.getPrecio());
        request.getSession().setAttribute("year", coche.getYear());
        request.getSession().setAttribute("descripcion", coche.getDescripcion());
        request.getSession().setAttribute("stock", coche.getStock());
        request.getSession().setAttribute("imagen", coche.getImagen());
        request.setAttribute("coche", coche);

        //coche.reset();

        return mapping.findForward("gestionCatalogo");
    }

     private void almacenarCoche(Coche coche){
        // Persistencia de datos
        ConnectBD.connectionBD();
       
        System.out.println("");
        ConnectBD.insertItemBD("INSERT INTO coche( marca, modelo, caracteristicas, precio, \"year\", descripcion, stock, imagen) values ('" + coche.getMarca() + "','" + coche.getModelo() + "','" + coche.getCaracteristicas() + "','" +
                                coche.getPrecio() + "','" + coche.getYear() + "','" + coche.getDescripcion() + "','" + coche.getStock() + "','" + coche.getImagen() + "')");
        ConnectBD.disconnectBD();

    }
}
